<?php

namespace IMT\Plugins\AttachmentsBundle\Controller;

use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class DefaultController extends Controller
{
    
    public function indexAction()
    {
        return $this->forward('IMTPluginsAttachmentsBundle:Default:show', array(
        	'idEvent' => 0
    	));
    }
    
    public function showAction($idEvent)
    {
    	$emEvent = $this->get('imt_event.event_manager');
    	$securityContext = $this->get('security.context');
    	$user = $securityContext->getToken()->getUser();
    	 
    	$participants = array();
    	
    	if($idEvent != 0){
    		$event = $emEvent->findEventBy(array('id' => $idEvent));
    		 
    		if(!$event)
    			throw $this->createNotFoundException('L\'événement recherché n\'existe pas');
    	} else 
    		$event = $emEvent->createEvent();
    	    	 
    	return $this->render('IMTPluginsAttachmentsBundle:Default:index.html.twig', array(
    			'event' => $event,
    	));
    }
    
    public function updateAJAXAction($idEvent)
    {
    	$return = array('result' => 500, 'msg' => 'Une erreur inconnue est survenue');
    
    	try {
    		$request = $this->getRequest();
    		$em = $this->getDoctrine()->getEntityManager();
    		 
    		if('POST' == $request->getMethod()){
    
    			$emEvent = $this->container->get('imt_event.event_manager');
    			$validator = $this->get('validator');
    			 
    			if($idEvent != 0){
    				$event = $emEvent->findEventBy(array('id' => $idEvent));
    			}
    			 
    			if(!$event)
    				throw $this->createNotFoundException('L\'événement à modifier n\'existe pas');
    			
//     			$repoParticipant = $this->getDoctrine()->getRepository('IMTPluginsParticipantsBundle:ParticipantEvent');
//     			$repoContact = $this->getDoctrine()->getRepository('IMTUserBundle:Contact');
    			
//     			$idContacts = $request->request->get('id_contacts');
//     			$nbEdits = 0;
    			
//     			// Recherche des participants déjà enregistrés et formation de la liste
//     			$listParticipantResult = $repoParticipant->findBy(array(
//     					"event" => $idEvent,
//     			));
    			
//     			$listParticipant = array();
//     			foreach($listParticipantResult as $participant){
//     				$listParticipant[$participant->getParticipant()->getId()] = $participant;
//     			}
    			
//     			// Traitement des contacts envoyés
//     			if(is_array($idContacts)){
// 	    			foreach($idContacts as $idContact){
// 		    			if(isset($listParticipant[$idContact])){
// 		    				$contact = $listParticipant[$idContact]->getParticipant();
// 		    				unset($listParticipant[$idContact]);
// 		    			} else {
// 		    				$contact = $repoContact->findOneBy(array("id" => $idContact));
			    			
// 			    			if(!$contact)
// 			    				throw $this->createNotFoundException('Un contact choisi n\'existe pas');
			    			
// 			    			$participantEvent = new ParticipantEvent();
// 			    			$participantEvent->setEvent($event);
// 			    			//$participantEvent->setAccount(NULL);
// 			    			$participantEvent->setParticipant($contact);
// 			    			$participantEvent->setParticipationState(0);
			    			
// 			    			$em->persist($participantEvent);
// 			    			$em->flush();
// 			    			$nbEdits++;
// 		    			}
// 	    			}
//     			}
    			
//     			foreach($listParticipant as $participantEvent){
// 	    			$em->remove($participantEvent);
// 	    			$em->flush();
// 	    			$nbEdits++;
//     			}
    				
//     			$return['result'] = 200;
//     			$return['msg'] = ($nbEdits > 1) ? 'Les participants ont été modifiés' : ($nbEdits > 0) ? 'Le participant a été modifié' : 'Aucune modification apporté aux participants';
//     			/*$return['participant'] = array(
//     					'state' => 0,
//     					'contact' => $contact->getJsonObject(false)
//     			);*/
//     			return new Response(json_encode($return));
    		} else {
    			throw new MethodNotAllowedException('POST');
    		}
    	} catch(NotFoundHttpException $e){
    		return new Response(json_encode(array('result' => 500, 'msg' => $e->getMessage())));
    	} catch(AccessDeniedException $e){
    		return new Response(json_encode(array('result' => 500, 'msg' => $e->getMessage())));
    	} catch(\Exception $e){
    		return new Response(json_encode(array(
    				'result' => 500,
    				'msg' => 'Une erreur est survenue au niveau du serveur',
    				'debug' => $e->getMessage()
    		)));
    	}
    	 
    	return new Response(json_encode($return));
    }
}
